package lyttest.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import lyttest.entity.dto.FileDTO;
import lyttest.entity.po.UserPO;
import lyttest.service.FileService;
import lyttest.uils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/v1")
public class TemplateFileController {
    @Autowired
    private FileService fileService;

    @PostMapping("/createOrUpdate")
    public Response<UserPO> createOrUpdate(@RequestBody  @Valid FileDTO fileDTO) {
        try {
            log.info("创建更新对象Request:{} ", JSON.toJSONString(fileDTO));
            return Response.createSuccess(fileService.createOrUpdate(fileDTO));
        } catch (Exception e) {
            log.error("创建更新对象，入参数: ", JSON.toJSONString(fileDTO), e);
            return Response.createError("创建更新对象失败");
        }
    }

    @PostMapping("/queryDetail")
    public Response<UserPO> queryDetail(@RequestBody FileDTO fileDTO) {
        try {
            log.info("查询详情Request:{} ", JSON.toJSONString(fileDTO));
            return Response.createSuccess(fileService.queryDetail(fileDTO));
        } catch (Exception e) {
            log.error("查询详情，入参数: ", JSON.toJSONString(fileDTO), e);
            return Response.createError("查询详情失败");
        }
    }

    @PostMapping("/delete")
    public Response<UserPO> delete(@RequestBody FileDTO fileDTO) {
        try {
            log.info("删除对象Request:{} ", JSON.toJSONString(fileDTO));
            return Response.createSuccess(fileService.delete(fileDTO));
        } catch (Exception e) {
            log.error("删除对象，入参数: ", JSON.toJSONString(fileDTO), e);
            return Response.createError("删除对象失败");
        }
    }

    @PostMapping("/page")
    public Response<UserPO> page(@RequestBody FileDTO fileDTO) {
        try {
            log.info("分页查询Request:{} ", JSON.toJSONString(fileDTO));
            return Response.createSuccess(fileService.page(fileDTO));
        } catch (Exception e) {
            log.error("分页查询，入参数: ", JSON.toJSONString(fileDTO), e);
            return Response.createError("分页查询失败");
        }
    }

}